//
//  FTEditScrollView.m
//  FashionTech
//
//  Created by JackieYip on 16/8/29.
//  Copyright © 2016年 mondial-d. All rights reserved.
//

#import "FTEditScrollView.h"

@interface FTEditScrollView () 
@end

@implementation FTEditScrollView

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self setupEditScrollView];
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        [self setupEditScrollView];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupEditScrollView];
    }
    return self;
}

- (void)setupEditScrollView
{
    // 设置scrollView的属性
    self.maximumZoomScale = 2.5;
    self.minimumZoomScale = 1;
    self.showsHorizontalScrollIndicator = NO;
    self.showsVerticalScrollIndicator = NO;
    
    // 添加图片
    self.imageView = [[UIImageView alloc] initWithFrame:self.bounds];
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.imageView.userInteractionEnabled = YES;
    [self addSubview:self.imageView];
    
    // 设置代理
    self.delegate = self;
    
    // 添加单击事件
    UITapGestureRecognizer *sigleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didSigleTapsOnScrollView:)];
    sigleTap.numberOfTapsRequired = 1;
    [self.imageView addGestureRecognizer:sigleTap];
    // 添加双击事件
    UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(zoomInOrOut:)];
    doubleTap.numberOfTapsRequired = 2;
    [self.imageView addGestureRecognizer:doubleTap];
    
    [sigleTap requireGestureRecognizerToFail:doubleTap];
}

#pragma mark - UIScrollView Delegate
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    if ([_editDelegate respondsToSelector:@selector(didZoomingToFrame:inTheEditScrollView:)])
        [_editDelegate didZoomingToFrame:_imageView.frame inTheEditScrollView:self];
    return _imageView;
}

#pragma mark - Target Action
- (void)didSigleTapsOnScrollView:(UITapGestureRecognizer *)tapGesture
{
    if ([_editDelegate respondsToSelector:@selector(didSigleTapsOnTheEditScrollView:)])
        [_editDelegate didSigleTapsOnTheEditScrollView:self];
}

- (void)zoomInOrOut:(UITapGestureRecognizer *)tapGesture
{
    if (self.zoomScale >= self.maximumZoomScale) {
        [self setZoomScale:1 animated:YES];
    }else {
        CGPoint point = [tapGesture locationInView:self];
        [self zoomToRect:CGRectMake(point.x - 40, point.y - 40, 80, 80) animated:YES];
    }
}
@end
